<?php

namespace app\admin\controller\subject;

use app\common\controller\Backend;

class Subject extends Backend
{
    protected $model = null;

    protected $relationSearch = true;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Subject.Subject');
        $this->CategoryModel = model('Subject.Category');
        $this->ChapterModel = model('Subject.Chapter');
        $this->TeacherModel = model('Subject.Teacher.Teacher');
        // 查询某一列的值并且指定索引
        $cate = $this->CategoryModel->select();
        $teacher = $this->TeacherModel->select();
        $catelist = [];
        $teacherlist = [];
        foreach ($cate as $item)
        {
            $catelist[$item['id']] =  $item['name'];
        }
        foreach ($teacher as $item)
        {
            $teacherlist[$item['id']] =  $item['name'];
        }
        $this->view->assign([
            'catelist' => $catelist,
            'teacherlist' => $teacherlist
        ]);
    }

    // 查看
    public function index()
    {
        if ($this->request->isAjax())
        {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                ->with(['category', 'teacher'])
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->with(['category', 'teacher'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    //添加
    public function add()
    {
        if ($this->request->isPost())
        {
            // 获取表单数据
            $params = $this->request->post('row/a');

            // 加校验器
            $result = $this->model->save($params);

            if ($result === false)
            {
                $this->error($this->model->getError());
                exit;
            }

            $this->success();
        }

        //先将课程分类查询出来
        // $catelist = $this->CategoryModel->column('id,name');
        // $this->assign('catelist', build_select('row[cateid]', $catelist, [], ['class'=>'selectpicker', 'required'=>'']));

        return $this->view->fetch();
    }

    // 编辑
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);

        if (!$row)
        {
            $this->error(__('课程不存在'));
        }

        if ($this->request->isPost())
        {
            // 获取表单数据
            $params = $this->request->post('row/a');
            $params['id'] = $ids;

            $result = $this->model->isUpdate(true)->save($params);

            if ($result === false)
            {
                $this->error($this->model->getError());
                exit;
            }

            //判断是否有上传新图片
            if ($row['thumbs'] != $params['thumbs'])
            {
                //不相等就说明有换图片了，就删除掉旧图片
                @is_file("." . $row['thumbs']) && @unlink("." . $row['thumbs']);
            }

            $this->success();
        }

        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    // 删除
    public function del($ids = null)
    {
        $row = $this->model->select($ids);

        if (!$row)
        {
            $this->error(__('课程不存在'));
        }

        $result = $this->model->destroy($ids);

        if ($result === false)
        {
            $this->error($this->model->getError());
            exit;
        }

        $this->success();
    }

    
    // 回收站列表
    public function recyclebin()
    {
        if ($this->request->isAjax())
        {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                ->onlyTrashed()
                ->with(['category'])
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->onlyTrashed()
                ->with(['category'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
    }

    // 还原
    public function restore($ids = null)
    {
        $row = $this->model->onlyTrashed()->select($ids);

        if (!$row)
        {
            $this->error(__('课程不存在'));
        }

        $result = $this->model->onlyTrashed()->where('id', 'IN', $ids)->update(['deletetime' => null]);

        if ($result === false)
        {
            $this->error($this->model->getError());
            exit;
        }

        $this->success();
    }

    // 销毁
    public function destroy($ids = null)
    {
        $row = $this->model->onlyTrashed()->select($ids);

        if (!$row)
        {
            $this->error(__('课程不存在'));
        }

        $urllist = $this->ChapterModel->where('subid', 'IN', $ids)->column('url');

        $result = $this->model->destroy($ids,true);

        if ($result === false)
        {
            $this->error($this->model->getError());
            exit;
        }

        // 删除课程图片
        foreach($row as $item)
        {
            @is_file("." . $item['thumbs']) && @unlink("." . $item['thumbs']);
        }

        // 删除课程章节视频
        foreach($urllist as $item)
        {
            @is_file("." . $item) && @unlink("." . $item);
        }

        $this->success();

    }
}
